package ModelLayer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


/**
 * Write a description of class LeaseCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeaseCollection
{
   private static ArrayList<Lease> leases;
   private static LeaseCollection instance;
   private static FileLease file;
    /**
     * Constructor for objects of class LeaseCollection
     */
    protected LeaseCollection()
    {
        file = new FileLease();
        leases = file.get_data();
    }
    public static void initialize()
    {
        if (instance == null)
        {
            instance = new LeaseCollection();
        }
    }
    
    public static LeaseCollection get_instance()
    {
        return instance;
    }
    
    public static void save_data()
    {
        file.save_changes(leases);
    }
    
    public static String create_lease(String return_date, ArrayList<String> machines, double total_cost)
    {
        int max_temp_id = 0;
        int temp_id = 0;
        String start_date = current_date();
        String id;
        for (Lease lease : leases)
        {
            temp_id = Integer.parseInt(lease.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(is_such_id(id = "" + (max_temp_id + 1))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        id = "" + (max_temp_id + 1);   
        leases.add(max_temp_id, new Lease(id, start_date, return_date, total_cost, machines, false));
        return id;
    }
    
    public static void close_lease(String id, double recalculate_cost)
    {
        Lease temp = find_by_id(id);
        temp.set_total_cost(recalculate_cost);
        temp.set_return_date(current_date());
        temp.set_is_closed(true);
    }
    
    public static void clear_machine(String id)
    {
        for(Lease lease : leases)
        {
            if(lease.get_ids().contains(id))
            {
                lease.clear_machine(id);
            }
        }
    }
    
    public static Object[][] display_all_leases()
    {
        Object[][] all_data = new Object[leases.size()][7];
        int i=0;
        for (Lease lease : leases)
        {
            all_data[i] = lease.give_array();
            i++;
        }
        return all_data;
    }
    
    public static boolean is_such_id(String id)
    {
        for(Lease lease : leases)
        {
            if(lease.get_id().equals(id))
            {
                return true;
            }
        }
        return false;
    }            

    
    private static int days_between(String start_date, String return_date)
    {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            sdf.setLenient(false);
            int diff_in_days = 0;
            try
            {
                Date date1 = sdf.parse(return_date);
                Date date2 = sdf.parse(start_date);
                diff_in_days = (int)( (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24));
            }
            catch (ParseException e) 
            {
                e.printStackTrace();
            }
            return diff_in_days;
    }
    
    public static boolean is_lease_closed(String id)
    {
        return find_by_id(id).get_is_closed();
    }
    
    private static String current_date()
    {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        return dateFormat.format(date.getTime());
    }
    
    public static boolean is_date_before(String id)
    {
        return (days_between(current_date(), find_by_id(id).get_return_date()) > 0);
    }
    
    public static boolean is_date_after(String id)
    {
        return (days_between(current_date(), find_by_id(id).get_return_date()) < 0);
    }
         
   public static Lease find_by_id(String id)
   {
         for (Lease lease : leases)
         {
            if(lease.get_id().equals(id))
            {
                return lease;
            }
         }
         return null;
   }

}
